// pages/activityCenter/jf2.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "交房预约", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    dateList: [],
    chooseDate: -1, //日期选择
    timeList: [{
        id: 0,
        name: '10:30- 12:30',
        status: true,
      },
      {
        id: 1,
        name: '13:30-16:30',
        status: true,
      },
      {
        id: 2,
        name: '13:30-16:30',
        status: true,
      },
      {
        id: 3,
        name: '13:30-16:30',
        status: false,
      },
      {
        id: 4,
        name: '13:30-16:30',
        status: false,
      }
    ],
    chooseTime: -1,
    roomInfo: '',
    //人数
    peopleNum: '请选择人数',
    peopleNumList: [],
    //留言
    partPro: '',
    noteMaxLen: 300,

    mySf: '',
    openid: '',
    shareTitle: '',
    shareImg: '',
    tipsContent: '',
    is_register: 0,
    id: 0,
    activityInfo: [],
    is_header_wj: 0,
    team: '',
    room_number: '',
    houseNum: ''
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  // 当前房产
  roomInfoIpt(e) {
    this.setData({
      roomInfo: e.detail.value
    })
  },
  //预约人数
  peopleNumChange(e) {
    let that = this;
    that.setData({
      peopleNum: that.data.peopleNumList[e.detail.value].name,
    })
  },
  //选择日期
  chooseDate(e) {

    let that = this;
    let id = e.currentTarget.dataset.id;

    var list = that.data.dateList;
    if (list[id]['status'] == '日期已过') {
      app.showTip('抱歉,您选择的时间日期已过了哦,请选择其他时间预约');
      return;
    } else if (list[id]['status'] == '预约已满') {
      app.showTip('抱歉,您选择的时间预约人数已满了哦,请选择其他时间预约');
      return;
    }

    //判断是否点击的是当前项
    if (id == that.data.chooseDate) {
      //是点击的当前项
      return;
    }

    //更改选择的时间状态
    that.setData({
      chooseDate: id
    });

    var dateList = that.data.dateList;
    wx.showLoading({
      title: '时间段状态切换中...'
    })
    //后台请求时段是否满了
    wx.request({
      url: that.data.baseUrl + 'checkActivitySjcIsBmLy.php',
      method: 'POST',
      data: {
        tid: dateList[id].id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          that.setData({
            timeList: res.data.timeList,
            chooseTime: -1
          });
        }

      },
      fail: function() {
        wx.hideLoading();
      }
    });

  },
  //时段选择
  chooseTime(e) {

    let that = this;
    let id = e.currentTarget.dataset.id;
    //判断是否预约已满了
    if (that.data.timeList[id].isBlack == 1) {
      return;
    }
    that.setData({
      chooseTime: id
    })

  },
  //留言
  partProVal(e) {
    this.setData({
      partPro: e.detail.value
    })
  },
  //描述输入框
  partPro: function(e) {
    var value = e.detail.value,
      len = parseInt(value.length);
    if (len > this.data.noteMaxLen) return;
    this.setData({
      currentNoteLen: len //当前字数
    })
  },
  //提交信息
  tj() {
    let that = this;

    //判断是否注册
    if (that.data.is_register == '0') {
      wx.showModal({
        title: '温馨提示',
        content: that.data.tipsContent,
        complete: (res) => {
          if (res.confirm) {
            //跳转注册界面
            wx.setStorageSync('loginHeaderUrl', '/pages/activityCenter/detail?id=' + that.data.id + "&isType=1");
            wx.setStorageSync('loginHeaderType', 'redirectTo')
            if (res.confirm) {
              //跳转注册界面
              wx.redirectTo({
                url: '/pages/login/select-login/select-login'
              })
            }
          }
        }
      })
      return;
    }

    let roomInfo = that.data.roomInfo;
    let peopleNum = that.data.peopleNum;
    let chooseDate = that.data.chooseDate;
    let chooseTime = that.data.chooseTime;

    if (roomInfo == '') {
      app.showTip("请填写当前房产");
      return;
    }
    if (peopleNum == '请选择人数') {
      app.showTip("请选择人数");
      return;
    }
    if (chooseDate == -1) {
      app.showTip("请选择日期");
      return;
    }
    if (chooseTime == -1) {
      app.showTip("请选择时段");
      return;
    }

    //判断是否跳转问卷
    if (that.data.is_header_wj == 1) {
      //跳转问卷
      wx.showLoading({
        title: '数据检测中...'
      });
      //后台请求时段是否满了
      wx.request({
        url: that.data.baseUrl + 'checkBmInfo.php',
        method: 'POST',
        data: {
          openid: that.data.openid,
          people_number: peopleNum,
          id: that.data.id,
          tid: that.data.dateList[chooseDate].id,
          sid: that.data.timeList[chooseTime].id,
          team: that.data.team,
          room_number: that.data.room_number,
          remark: that.data.partPro
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          wx.hideLoading();
          if (res.data.status == 1) {
            //清空到问卷页的缓存
            wx.setStorageSync('orderArr', {});
            var  timeInfo='';
            if(that.data.activityInfo['sd_type']==2){
              //固定日期
              timeInfo = that.data.courseTimeList[that.data.courseTimeId].startTime+" "+that.data.courseTimeList[that.data.courseTimeId].startSd+"~"+that.data.courseTimeList[that.data.courseTimeId].endTime+" "+that.data.courseTimeList[that.data.courseTimeId].endSd;
            }else{
              //灵活时段 or 固定时段
              timeInfo = that.data.dateList[that.data.chooseDate].time+ " " + that.data.timeList[that.data.chooseTime].sjd;
            }
            //缓存订单信息
            wx.setStorageSync('orderArr', {
              id: that.data.id,
              tid: that.data.dateList[chooseDate].id,
              sid: that.data.timeList[chooseTime].id,
              people_number: peopleNum,
              team: that.data.team,
              room_number: that.data.room_number,
              remark: that.data.partPro,
              timeInfo:timeInfo
            });
            wx.redirectTo({
              url: '/pages/activityCenter/question'
            });

          } else {
            app.showTip(res.data.msg);
          }

        },
        fail: function() {
          wx.hideLoading();
          app.showTip(服务器繁忙, 请稍候重试);
        }
      });
    } else {
      //直接提交信息
      wx.showLoading({
        title: '信息提交中...'
      });
      //后台请求时段是否满了
      wx.request({
        url: that.data.baseUrl + 'addActivityBmInfo.php',
        method: 'POST',
        data: {
          openid: that.data.openid,
          people_number: peopleNum,
          id: that.data.id,
          tid: that.data.dateList[chooseDate].id,
          sid: that.data.timeList[chooseTime].id,
          team: that.data.team,
          room_number: that.data.room_number,
          remark: that.data.partPro
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          wx.hideLoading();
          if (res.data.status == 1) {
            wx.redirectTo({
              url: '/pages/activityCenter/bmResult?title=' + that.data.activityInfo.title + "&time=" + that.data.dateList[that.data.chooseDate].time + " " + that.data.timeList[that.data.chooseTime].sjd + "&address=" + that.data.activityInfo.address
            });
          } else {
            app.showTip(res.data.msg);
          }
        },
        fail: function() {
          wx.hideLoading();
          app.showTip(服务器繁忙, 请稍候重试);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var id = options.id;
    var team = options.team;
    var room_number = options.room_number
    //id = 1;
    if (id == '' || id == '0') {
      app.showTip('抱歉,未获取到相关的活动id信息哦');
      setTimeout(function() {
        wx.navigateBack({
          delta: 1
        });
      }, 2000);
      return;
    }
    this.setData({
      id: id
    });

    //判断是否是非业主验证来的
    if (team != undefined && room_number != undefined) {
      that.setData({
        team: team,
        room_number: room_number,
        roomInfo: team + '' + room_number
      });
    }

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function(res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getActiveBmPageInfo(res.data.openid, id);
              },
              fail: function(res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getActiveBmPageInfo(openid, id);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getActiveBmPageInfo(openid, id) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getActiveBmPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'id': id,
        'team': that.data.team,
        'room_number': that.data.room_number
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {

          //重新赋值后台请求数据
          that.setData({
            is_register: res.data.is_register,
            mySf: res.data.mySf,
            tipsContent: res.data.tipsContent,
            activityInfo: res.data.activityInfo,
            dateList: res.data.dateList,
            timeList: res.data.timeList,
            chooseDate: res.data.chooseDate,
            is_header_wj: res.data.is_header_wj,
            shareTitle: res.data.activityInfo.title,
            shareImg: that.data.baseUrl + 'admin/' + res.data.activityInfo.img_path,
            peopleNumList: res.data.peopleList,
            houseNum: res.data.houseNum
          });


          //处理组团楼栋信息
          if (that.data.team == '' || that.data.room_number == '') {
            that.setData({
              roomInfo: res.data.team + '' + res.data.room_number,
              team: res.data.team,
              room_number: res.data.room_number
            });
          }
        } else {
          app.showTip(res.data.msg);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });

  },


  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: that.data.shareTitle,
      path: '/pages/activityCenter/detail?type=1' + "&id=" + that.data.id + '&is_share=1',
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})